import 'package:flutter/material.dart';

void main() {
  runApp(const MaterialApp(title: 'Flutter第二天',home: Counter(),));
}

class CounterDisplay extends StatelessWidget {
  const CounterDisplay({Key? key, required this.count}) : super(key: key);

  final int count;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text('按钮点击$count次'),
    );
  }
}

class CounterIncrementor extends StatelessWidget {
  const CounterIncrementor({Key? key, required this.onPressed})
      : super(key: key);

  final VoidCallback onPressed;

  @override
  Widget build(BuildContext context) {
    return FloatingActionButton(
      onPressed: onPressed,
      tooltip: '增加',
      child: const Icon(Icons.add),
    );
  }
}

class Counter extends StatefulWidget {
  const Counter({Key? key}) : super(key: key);

  @override
  _CounterState createState() => _CounterState();
}

class _CounterState extends State<Counter> {
  int _count = 0;

  void _increment() {
    setState(() {
      _count += 1;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('数字增加'),
      ),
      body: CounterDisplay(count: _count,),
      floatingActionButton: CounterIncrementor(onPressed: _increment,),
    );
  }
}
